package com.zf.common.http.entity;

import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;


/**
 * 自定义 MultipartFile
 */
public class CustomMultipartFile implements MultipartFile {
        private final byte[] byteArray;
        private final String filename;

        public CustomMultipartFile(byte[] byteArray, String filename) {
            this.byteArray = byteArray;
            this.filename = filename;
        }

        @Override
        public String getName() {
            return this.filename;
        }

        @Override
        public String getOriginalFilename() {
            return this.filename;
        }

        @Override
        public String getContentType() {
            return "";
        }

        @Override
        public boolean isEmpty() {
            return this.byteArray.length == 0;
        }

        @Override
        public long getSize() {
            return this.byteArray.length;
        }

        @Override
        public byte[] getBytes() throws IOException {
            return this.byteArray;
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(this.byteArray);
        }

        @Override
        public void transferTo(File dest) throws IOException, IllegalStateException {
            return;
        }
    }
